@tool
extends Resource


class_name AttributeSet
"""
用户自定义的可编辑属性组
"""


## 属性变更通知
signal event_property_changed(attrs: AttributeSet, attr: Attribute, property: String, new_value: int)


## 自定义属性值
@export var items: Dictionary[String, Attribute] = {}


var hooks: Dictionary[String, Callable] = {}
var is_hooked: bool = false


func __on_property_changed(attr: Attribute, property: String, new_value: int, attr_name: String) -> void:
    event_property_changed.emit(self, attr, attr_name + "_" + property, new_value)

func enable_notify() -> void:
    if not is_hooked:
        hooks.clear()
        for k in items:
            var f: Callable = __on_property_changed.bind(k)
            items[k].event_property_changed.connect(f)
            hooks[k] = f
        is_hooked = true

func disable_notify() -> void:
    if is_hooked:
        for k in items:
            items[k].event_property_changed.disconnect(hooks[k])
        hooks.clear()
        is_hooked = false

func pick() -> Dictionary[String, int]:
    var v: Dictionary[String, int] = {}
    for k in items:
        v[k] = items[k].pick()
    return v

func clone() -> AttributeSet:
    var c: AttributeSet = AttributeSet.new()
    for k in items:
        c.items[k] = items[k].duplicate()
    return c

func merge(other: AttributeSet) -> void:
    for k in other.items:
        if not items.has(k):
            items[k] = other.items[k].duplicate()
